import {
    getUserOrderList,
    addOrderComment,
    getSystemSelect,
    cancelOrDeleteOrder,
    cancelOrder
} from '../../../api/index'

export default {
    props: {
        isMore: Boolean,
        handleIsDetails: Function,
        activeName: String,
        search: String
    },
    data() {
        return {
            BaseUrl: process.env.VUE_APP_WAREHOUSE_API,
            formInline: { // 搜索条件
                timeFrame: [],
                transition: '',
                system: '',
                countdownTime: ''
            },
            commentForm: { // 评论
                comment: '',
                star: 5,
                commodityId: '', //商品id
                orderShopId: '' // 商品订单Id
            },

            realStar: 5, // 实心个数
            allChecked: false, // 全选
            src: require('../../../assets/test02.jpg'), // 商品封面
            page: 1, // 分页当前页
            pageSize: 10, // 分页显示条数
            total: 0, // 分页总条数
            commentVisible: false, // 评论弹窗
            cancelDialogVisible: false, // 取消订单弹窗
            cancelOrder: '', //  取消订单弹窗理由选择器
            isDisabled: false, // 交易状态是否禁用
            userOrderList: [], // 订单列表
            selectOrderList: [], // 选择后的保存
            SystemSelect: [], // 系统选择下拉框
        }
    },
    methods: {
        // 每页显示条数
        handleSizeChange(val) {
            this.pageSize = val
            this.handleGetOrderList()
        },

        // 当前页
        handleCurrentChange(val) {
            this.page = val
            this.handleGetOrderList()
        },

        // 搜索
        handleClickSearch(val) {
            this.handleGetOrderList()
        },

        // 判断当前tab
        handleOrderType() {
            this.page = 1 // 分页当前页
            this.pageSize = 10 // 分页显示条数
            this.total = 0  // 分页总条数
            switch (this.activeName) {
                case 'soTheOrder': // 所有订单
                    this.isDisabled = false
                    this.formInline.transition = ''
                    this.handleGetOrderList()
                    break
                case 'bePaid': // 待付款
                    this.isDisabled = true
                    this.formInline.transition = '1'
                    this.handleGetOrderList()
                    break
                case 'Paid': // 已付款
                    this.isDisabled = true
                    this.formInline.transition = '2'
                    this.handleGetOrderList()
                    break
                case 'continuationFee': // 待续费
                    this.isDisabled = true
                    this.formInline.transition = '4'
                    this.handleGetOrderList()
                    break
                case 'cancelled': // 已取消
                    this.isDisabled = true
                    this.formInline.transition = '3'
                    this.handleGetOrderList()
                    break
                default:
                    break
            }
        },

        // 发起请求获取订单列表
        handleGetOrderList() {
            this.allChecked = false
            this.selectOrderList = []
            const loading = this.$loading({
                lock: true,
                text: '正在获取数据...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            })
            let endTime = '',
                startTime = ''
            if (this.formInline.timeFrame){
                endTime =  this.formInline.timeFrame[1]
                startTime  =  this.formInline.timeFrame[0]
            }
            let order = {
                dayTime: this.formInline.countdownTime,
                endTime: endTime,
                keyword: this.search,
                projectIid: this.formInline.system,
                startTime: startTime,
                status: this.formInline.transition == 0 ? '' : this.formInline.transition * 1
            }
            getUserOrderList(order, this.page, this.pageSize).then(res => {
                if (res.data.code === 200) {
                    res.data.result.list.forEach(item => {
                        item.ischeck = false
                    })
                    this.userOrderList = res.data.result.list
                    this.total = res.data.result.total
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
                loading.close()
            }).catch(err => {
                loading.close()
                // this.$message.error('获取订单列表失败！' + err)
            })
        },

        // 全选
        handleCheckedAll(val) {
            // 将所有商品切换成与全选按钮一致
            this.userOrderList.forEach(item => {
                item.ischeck = val
            })
            // 全选为勾选状态则将所有数据保存到暂存数组中，否则清空暂存数组
            if (val) {
                this.selectOrderList = this.userOrderList
            } else {
                this.selectOrderList = []
            }
        },

        // 商品单选
        handleSystemChange(row) {
            // 如果当前为选择状态则将该条信息保存到selectOrderList中，如果是取消选择贼从selectOrderList中删除该条数据
            if (row.ischeck) {
                this.selectOrderList.push(row)
            } else {
                this.selectOrderList = this.selectOrderList.filter(item => item.orderNo != row.orderNo)
            }

            // 如果当前选择条数与请求出来的数据条数一致则勾选全选否则取消
            if (this.selectOrderList.length === this.userOrderList.length) {
                this.allChecked = true
            } else {
                this.allChecked = false
            }
        },

        // 评论星级
        handleCommentStar(index) {
            this.commentForm.star = index
        },

        // 评论弹窗
        handleCommentVisible(commodityId, orderShopId) {

            this.commentForm.commodityId = commodityId
            this.commentForm.orderShopId = orderShopId

            this.commentVisible = true
        },

        // 评论
        handleCommentOrder() {
            const loading = this.$loading({
                lock: true,
                text: '正在评论...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            })
            addOrderComment({
                commentGrade: this.commentForm.star, // 评论星级
                commentContent: this.commentForm.comment, // 评论内容
                commodityId: this.commentForm.commodityId, //商品ID
                orderShopId: this.commentForm.orderShopId //商品订单ID
            }).then(res => {
                if (res.data.code === 200) {
                    this.$message({
                        message: '评论成功',
                        type: 'success'
                    })
                    let result = {
                        order: {
                            dayTime: 0,
                            endTime: "",
                            keyword: "",
                            projectIid: "",
                            startTime: "",
                            status: 0
                        },
                        page: this.page,
                        pageSize: this.pageSize
                    }
                    this.handleGetOrderList(result)
                } else {
                    this.$message(res.data.msg)
                }
                this.commentForm = {
                    star: 5,
                    comment: '',
                    commodityId: '',
                    orderShopId: ''
                }
                this.commentVisible = false
                loading.close()
            }).catch(err => {
                loading.close()
                //this.$message.error('评论失败'+err);
            })
        },

        // 系统选择下拉框
        handleSystemSelect() {
            getSystemSelect({
                page: 1,
                pageSize: 100
            }).then(res => {
                if (res.data.code === 200) {
                    this.SystemSelect = res.data.result.list
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(err => {
                // this.$message.error('获取系统下拉框失败！',err)
            })
        },

        // 取消订单请求
        handleCancelOrDeleteOrder(deNumberArr) {
            const loading = this.$loading({
                lock: true,
                text: '正在删除...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            })
            cancelOrDeleteOrder(deNumberArr).then(res => {
                if (res.data.code === 200) {

                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })

                    this.handleGetOrderList()
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
                loading.close()
            }).catch(err => {
                loading.close()
                // this.$message.error('取消或删除订单失败')
            })
        },

        // 批量删除订单
        handleDatchOrderNumber() {
            if (this.selectOrderList.length <= 0) {
                this.$message('请选择需要删除的订单')

            } else {
                this.$confirm('确定删除这些订单吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let DeleteNumbers = []

                    this.selectOrderList.forEach(item => {
                        DeleteNumbers.push(item.orderNo)
                    })

                    this.handleCancelOrDeleteOrder(DeleteNumbers)
                }).catch(() => {

                })
            }

        },

        // 删除单个订单
        handleDeleteOnOrder(orderNo) {
            this.$confirm('确定删除“' + orderNo + '”这个订单吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let DeleteNumbers = [orderNo]
                this.handleCancelOrDeleteOrder(DeleteNumbers)
            }).catch(() => {

            })
        },

        handleCancelOrder(orderNo){
            this.$confirm('确定取消“' + orderNo + '”这个订单吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                const loading = this.$loading({
                    lock: true,
                    text: '正在取消...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                })
                cancelOrder([orderNo]).then(res=>{
                    if (res.data.code === 200) {

                        this.$message({
                            message: '取消成功',
                            type: 'success'
                        })

                        this.handleGetOrderList()
                    } else {
                        this.$message({
                            message: res.data.msg,
                            type: 'warning'
                        })
                    }
                    loading.close()
                }).catch(error => {
                    this.$message.error('取消失败')
                    loading.close()
                })
            }).catch(() => {

            })

        },


        // 再次购买 || 立即续费 || 立即购买
        handleContinueBuy(orderNo) {
            this.$router.push({
                path: '/order',
                query: {
                    orderNumber: orderNo
                }
            })
        },

        // 进入服务 || 进入课堂
        handleGoService(url) {
            if (url) {
                window.open(url)
            }
        }
    },
    created() {
        this.handleOrderType()
        this.handleSystemSelect()
    }
}
